package com.glorious.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.glorious.dto.response.system.SysUserAuthResponse;
import com.glorious.entities.SysUser;
import com.glorious.exception.NormalException;
import com.glorious.mapper.SysUserMapper;
import com.glorious.service.ISysUserService;
import org.springframework.stereotype.Service;

@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {

    @Override
    public SysUserAuthResponse getUserByUsername(String username) {
        SysUser sysUser = baseMapper.selectOne(Wrappers.<SysUser>lambdaQuery().eq(SysUser::getUsername, username).last("LIMIT 1"));
        if (ObjectUtil.isNull(sysUser)) {
            throw new NormalException("未查到用户");
        }
        SysUserAuthResponse response = new SysUserAuthResponse();
        BeanUtil.copyProperties(sysUser, response);
        response.setUserId(sysUser.getId());
        return response;
    }
}
